import { Component, OnInit, Injector, ViewChild, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    EditDtoExtensionsItem,
    Stock_BaseFieldListDto,
    StockZT_RoomServiceProxy,
    StockZT_LocationServiceProxy,
    StockZT_IntoBillItemServiceProxy,
    Stock_IntoToolingItemEditDto,
    LabelValue
} from '@shared/service-proxies/stock-service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { UserDataPermissionServiceProxy } from '@shared/service-proxies/stock-service-proxies';

interface SelectedItems {
    name: string;
    code: string;
    id: string;
}
@Component({
    selector: 'createOrEditToolingIntoBillItemModal',
    templateUrl: './create-or-edit-tooling-into-bill-item-modal.component.html'
})
export class CreateOrEditToolingIntoBillItemModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _billService: StockZT_IntoBillItemServiceProxy,
        private _stockZT_RoomServiceProxy: StockZT_RoomServiceProxy,
        private _stockZT_LocationServiceProxy: StockZT_LocationServiceProxy,
        private _userDataPermissionService: UserDataPermissionServiceProxy
    ) {
        super(injector);
    }

    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;

    editDto: Stock_IntoToolingItemEditDto;
    extensions: Array<EditDtoExtensionsItem>;
    optionAll;
    f_Warehouse: LabelValue;
    f_StorageArea: LabelValue;
    roomOption;
    f_StorageAreaCodeOption;
    insertid;
    ngOnInit() {
        
    }

    show(billId: string, id?: string, basefieldinfo?: Stock_BaseFieldListDto, insertid?: string): void {
        if (!billId) {
            return;
        }
        if (!basefieldinfo) {
            return;
        }

        this.active = true;
        this.insertid = insertid;

        this._billService.getToolingRoomList().subscribe(result => {
            this.roomOption = result.items.map(e => {
                return {
                    label: e.label,
                    value: e
                };
            });

            if (result.items.length >= 1) {
                                           
                                               this.f_Warehouse =
                                                   result.items[0];
 
                                               this.getStorage();
                                           }
            this._billService.getIntoToolingItemForEditExtension(id).subscribe(result => {
                let optionAll = {};

                this.optionAll = optionAll;

                this.editDto = result.stock_IntoToolingItemEditDto;

                this.editDto.f_BillId = billId;

                if (id == null) {
                    this.editDto.f_MaterialCode = basefieldinfo.f_MaterialCode;
                    this.editDto.f_MaterialName = basefieldinfo.f_MaterialName;
                    this.editDto.f_UnitPrice = basefieldinfo.f_Price;    
                    this.editDto.f_Specification = basefieldinfo.f_Specification;
                    
                    this.editDto.f_CAL_ToolingPictureId = basefieldinfo.f_CAL_ToolingPictureId;
                    this.editDto.f_CAL_ProductModel = basefieldinfo.f_CAL_ProductModel;
                    this.editDto.f_CAL_ProductPictureId = basefieldinfo.f_CAL_ProductPictureId;    
                    this.editDto.f_CAL_ToolingWeight = basefieldinfo.f_CAL_ToolingWeight;
                    this.editDto.f_CAL_ToolingMaxLoad = basefieldinfo.f_CAL_ToolingMaxLoad;
                    this.editDto.f_CAL_ProductModelRemark = basefieldinfo.f_CAL_ProductModelRemark;
                    this.editDto.f_CAL_IsAttrition = basefieldinfo.f_CAL_IsAttrition;
                    // this.editDto.f_Precision = basefieldinfo.f_CAL_ItemPrecision;
                    // this.editDto.f_Trademark = basefieldinfo.f_TradeMark;
                    // this.editDto.f_Hardness = basefieldinfo.f_CAL_ItemHardDegree;
                }
                this.editDto.f_WarehouseCode = this.f_Warehouse.value;
                this.editDto.f_WarehouseName = this.f_Warehouse.label;
                this.extensions = result.items;
                this.modal.show();
            });
        });
      

       
    }

    getStorage() {
        if (this.active != true) {
            return;
        }
        this._stockZT_LocationServiceProxy.getLocationListByWarehouseCode(this.f_Warehouse.value).subscribe(result => {
            this.f_StorageAreaCodeOption = result.items.map(e => {
                return {
                    label: e.label,
                    value: e
                };
            });
            if (result.items.length > 0) {
                //console.log(result.items[0]);
                this.f_StorageArea = result.items[0];
                this.editDto.f_StorageAreaCode = this.f_StorageArea.value;
                this.editDto.f_StorageAreaName = this.f_StorageArea.label;
                //console.log(this.f_Warehouse);
            }
            console.log(this.f_StorageArea);
        });

        // this.editDto.f_WarehouseCode = this.f_Warehouse.value;
        // this.editDto.f_WarehouseName = this.f_Warehouse.label;
    }
    getAreaInfo() {
        if (this.active != true) {
            return;
        }
        this.editDto.f_StorageAreaCode = this.f_StorageArea.value;
        this.editDto.f_StorageAreaName = this.f_StorageArea.label;
    }

    save(): void {
        if (this.editDto.f_Quantity <= 0) {
            this.message.warn('数量必须大于0');
        } else {
            this.saving = true;
            if (this.insertid) {
                this.editDto.id = this.insertid;
                this._billService
                    .insertToolingIntoItem(this.editDto)
                    .pipe(
                        finalize(() => {
                            this.saving = false;
                        })
                    )
                    .subscribe(() => {
                        this.notify.info(this.l('插入成功！'));
                        this.close();

                        this.modalSave.emit(null);
                    });
            } else {
                this._billService
                    .createOrUpdateIntoToolingItem(this.editDto)
                    .pipe(
                        finalize(() => {
                            this.saving = false;
                        })
                    )
                    .subscribe(() => {
                        this.notify.info(this.l('SavedSuccessfully'));
                        this.close();
                        this.modalSave.emit(null);
                    });
            }
        }
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
